const user = JSON.parse(sessionStorage.getItem("user"));


//更新登录状态
function update_user() {

	//获取session 里的参数
	let str = sessionStorage.getItem("user");
	let data = JSON.parse(str);
	//获取userinfo的内容
	let info = $("#userinfo");

	if (str != null) {
		let info = document.getElementById("userinfo");
		//清空数据
		info.innerHTML = '';
		let a = document.createElement("a");
		a.setAttribute("href", "mine.html");
		let span = document.createElement("span");
		let text = document.createTextNode(data.admin);
		let head_img = document.createElement("img");
		head_img.setAttribute("src", data.head_img);
		head_img.setAttribute("class", "head_img");
		span.appendChild(text);
		a.appendChild(span)
		a.appendChild(head_img)
		info.appendChild(a)

	}

}


//检测是否登录

function check_login() {
	if (user == null) {
		alert("您当前还未登录");
		location.href = "index.html";
	}
}


//转换时间格式
function formatTime(seconds) {
	var hours = Math.floor(seconds / 3600);
	var minutes = Math.floor((seconds - hours * 3600) / 60);
	var seconds = Math.floor(seconds - hours * 3600 - minutes * 60);
	return hours + ":" + padZero(minutes) + ":" + padZero(seconds);
}

function padZero(num) {
	return num < 10 ? "0" + num : num;
}

function showHead() {

	//获取session 里的参数
	let str = sessionStorage.getItem("user");
	let data = JSON.parse(str);


	let headStr = `
            <div class="header-content">
                <nav class="navbar navbar-expand">
                    <div class="collapse navbar-collapse justify-content-between">
                        <div class="header-left">
							<div class="dashboard_bar">
                                Dashboard
                            </div>
                        </div>
                        <ul class="navbar-nav header-right">
							
						
                            <li class="nav-item dropdown header-profile">
                                <a class="nav-link" href="javascript:void(0);" role="button" data-bs-toggle="dropdown">
                                    <img src="static/picture/pic1.jpg" alt="">
									<div class="header-info ms-3">
										<span>${data.account}</span>
										<small>${data.adminName}</small>
									</div>
                                </a>
                                <div class="dropdown-menu dropdown-menu-end">
                                  
                                    <a href="javascript:;" class="dropdown-item ai-icon" onclick="logout()">
                                        <svg id="icon-logout" xmlns="http://www.w3.org/2000/svg" class="text-danger" width="18" height="18" viewbox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line></svg>
                                        <span class="ms-2">Logout </span>
                                    </a>
                                </div>
                            </li>
                        </ul>
                    </div>
				</nav>
			</div>`;
			
			
	let header = document.getElementById("header");
	header.innerHTML = headStr;
	
}
function showLeftMenu(action){
	let nav = $(".deznav");
	let navStr = ` <div class="deznav-scroll">
				<ul class="metismenu" id="menu">
                    <li><a href="href?to=main" class="has-arrow ai-icon" href="javascript:void()" aria-expanded="false" id="dashboard">
							<i class="flaticon-025-dashboard"></i>
							<span class="nav-text">开单页</span>
						</a>
                    </li>
					
                    <li><a class="has-arrow ai-icon" href="javascript:void()" aria-expanded="false" id="set">
						<i class="flaticon-050-info"></i>
							<span class="nav-text">记录</span>
						</a>
                        <ul aria-expanded="false">
                            <li><a href="href?to=meal_list">开单记录</a></li>
                            
                        </ul>
                    </li>
					
                    <li style="display:none">
					 <a class="has-arrow ai-icon" href="javascript:void()" aria-expanded="false" id="report">
							<i class="flaticon-072-printer"></i>
							<span class="nav-text">报告管理</span>
						</a>
                        <ul aria-expanded="false">
                            <li><a href="form-element.html">报告列表</a></li>
                            <li><a href="form-wizard.html">总检审核</a></li>
                        </ul>
					</li>
                </ul>
				<div class="copyright">
					<p><strong>Innap Hotel Admin</strong> © 2021 All Rights Reserved</p>
					<p class="fs-12">Made with <span class="heart"></span> by DexignZone</p>
				</div>
			</div>`;
			
			
		nav.html(navStr);
}

function updateLeftMenuSelect(action){
	let menu = document.getElementById(action);
	menu.parentNode.setAttribute("class","mm-active");
	
	if(menu.parentNode.children.length == 2){
		menu.parentNode.children[1].setAttribute("class","mm-collapse mm-show");
	}
	
	menu.setAttribute("aria-expanded","true");
}


function timestampToDatetime(timestamp) {
	
  // 创建一个新的 Date 对象，将时间戳作为参数传递给构造函数
  var date = new Date(timestamp*1000);
	console.log(date)
  // 使用 Date 对象的方法获取年、月、日、小时、分钟和秒
  var year = date.getFullYear();
  var month = ("0" + (date.getMonth() + 1)).slice(-2);
  var day = ("0" + date.getDate()).slice(-2);
  var hours = ("0" + date.getHours()).slice(-2);
  var minutes = ("0" + date.getMinutes()).slice(-2);
  var seconds = ("0" + date.getSeconds()).slice(-2);

  // 拼接成特定的日期时间格式
  var datetime = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;

  return datetime;
}


function saveAsImage() {
  var myDiv = document.getElementById('print');

  // 创建一个新的 Canvas 元素
  var canvas = document.createElement('canvas');
  canvas.width = myDiv.offsetWidth;
  canvas.height = myDiv.offsetHeight;

  // 获取 Canvas 的上下文
  var context = canvas.getContext('2d');

  // 使用 div 的内容绘制到 Canvas 上
  html2canvas(myDiv).then(function(canvas) {
    // 将 Canvas 转换为图片数据
    var dataURL = canvas.toDataURL('image/png');

    // 创建一个下载链接
    var downloadLink = document.createElement('a');
    downloadLink.href = dataURL;
    downloadLink.download = 'div_image.png';

    // 将下载链接添加到文档中，并模拟点击下载
    document.body.appendChild(downloadLink);
    downloadLink.click();

    // 清理下载链接
    document.body.removeChild(downloadLink);
  });
}

function logout(){
	$.ajax("adminUser/logout", {
	    type: "GET",
	    success: function(data, textStatus) {
	           
	            alert("退出成功");
				location.href="href?to="+data;
	           
	    }
	})
}